<?php

//require_once  __DIR__ . '/includes/authenticate.php';

if (isset($_POST['season']) && isset($_POST['gamenumber']) && isset($_POST['playerID'])) {
	
foreach ($_POST as $key => $value) {
	if ($value == '') {
      $value = ":null";
      $$key = $value;
     } else {
    	$$key = $value;
     }
  }

  if ($season == '2017/2018') {

$sql =  "UPDATE stats 
SET ORB = $ORB, DRB = $DRB, ST = $ST, TOV = $TOV, PF = $PF, FGscored = $FGscored, FGtotal = $FGtotal, FTscored = $FTscored, FTtotal = $FTtotal, DNP = '$DNP', FG_outside_scored = $FGoutsidescored, FG_outside_total = $FGoutsidetotal, THPscored = $THPscored, THPtotal = $THPtotal
WHERE playerID = '$playerID' 
AND gamenumber = '$gamenumber' 
AND season = '$season'";

  } else if ( $season >= '2018/2019'){

        $MPsplit = explode(":", $MP);
        $MP = 60 * (int)$MPsplit[0] + (int)$MPsplit[1];

        $sql =  "UPDATE stats 
SET ORB = $ORB, DRB = $DRB, ST = $ST, TOV = $TOV, PF = $PF, FGscored = $FGscored, FGtotal = $FGtotal, FTscored = $FTscored, FTtotal = $FTtotal, DNP = '$DNP', FG_outside_scored = $FGoutsidescored, FG_outside_total = $FGoutsidetotal, THPscored = $THPscored, THPtotal = $THPtotal, MP = $MP, AST = $AST, BS = $BS, S5 = '$S5'
WHERE playerID = '$playerID' 
AND gamenumber = '$gamenumber' 
AND season = '$season'";


  } else {

$sql = "UPDATE stats 
SET ORB = $ORB, DRB = $DRB, ST = $ST, TOV = $TOV, PF = $PF, FGscored = $FGscored, FGtotal = $FGtotal, FTscored = $FTscored, FTtotal = $FTtotal, DNP = '$DNP'
WHERE playerID = '$playerID' 
AND gamenumber = '$gamenumber' 
AND season = '$season'";

}


try{

 
    $dbpdo = new PDO('mysql:host=localhost;dbname=basketbal','cas','cas');
    $dbpdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbpdo->prepare($sql);
	  $stmt->bindValue(":null", 0, PDO::PARAM_INT);
    $stmt->execute();
	if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
	} else {
	echo "<br><br><br><br><center> Player stats successfully updated </center>";
}
} catch (PDOException $e) {
    
    $error = $e->getMessage();
}

}

?>